<?php
namespace Product\Model;

use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
use Zend\Validator\File\IsImage;

class Product implements InputFilterAwareInterface
{
    public $id;
    public $name;
    public $description;
    public $category;
    public $manufacture;
    public $alias;
    public $preview_img;
    public $price;
    public $last_update;
    public $disabled;
    
    protected $inputFilter;


    public function exchangeArray($data)
    {
		$this->id			= (isset($data['id'])) ? $data['id'] : null;
		$this->name			= (isset($data['name'])) ? $data['name'] : null;
		$this->description	= (isset($data['description'])) ? $data['description'] : null;
		$this->category		= (isset($data['category'])) ? $data['category'] : null;
		$this->manufacture	= (isset($data['manufacture'])) ? $data['manufacture'] : null;
		$this->alias		= (isset($data['alias'])) ? $data['alias'] : null;
		$this->preview_img	= (isset($data['preview_img'])) ? $data['preview_img'] : null;
		$this->price		= (isset($data['price'])) ? $data['price'] : null;
		$this->last_update	= (isset($data['last_update'])) ? $data['last_update'] : null;
		$this->disabled		= (isset($data['disabled'])) ? $data['disabled'] : null;
    }
    
    public function setInputFilter(InputFilterInterface $inputFilter)
    {
		throw new \Exception('Not used');
    }
    
    public function getInputFilter()
    {
		if (!$this->inputFilter) {
			$inputFilter = new InputFilter();
			$factory = new InputFactory();
			$inputFilter->add($factory->createInput(array(
			'name' => 'id',
			'required' => true,
			'filters' => array(
				array('name' => 'Int'),
			),
			)));

			$inputFilter->add($factory->createInput(array(
				'name' => 'name',
				'required' => true,
				'filters' => array(
					array('name' => 'StripTags'),
					array('name' => 'StringTrim'),
				),
				'validators' => array(
					array(
						'name' => 'StringLength',
						'options' => array(
							'encoding' => 'UTF-8',
							'min' => 1,
							'max' => 40,
						),
					),
				),
			)));

			$inputFilter->add($factory->createInput(array(
				'name' => 'description',
				'required' => false,
				'filters' => array(
					array('name' => 'StripTags'),
					array('name' => 'StringTrim'),
				),
			)));

			$inputFilter->add($factory->createInput(array(
				'name' => 'manufacture',
				'required' => true,
				'fitlers' => array(
					array('name' => 'StripTags'),
					array('name' => 'StringTrim'),
				),
				'validators' => array(
					array(
					'name' => 'StringLength',
					'min' => 1,
					'max' => 50,
					),
				),
			)));

			$inputFilter->add($factory->createInput(array(
				'name' => 'alias',
				'required' => true,
				'fitlers' => array(
					array('name' => 'StripTags'),
					array('name' => 'StringTrim'),
				),
				'validators' => array(
					array(
					'name' => 'StringLength',
					'min' => 1,
					'max' => 50,
					),
				),
			)));

			$inputFilter->add($factory->createInput(array(
				'name' => 'preview_img',
				'required' => false,
				'filters' => array(
					array(
					'name' => 'Zend\Filter\File\RenameUpload',
					'options' => array(
						'target' => $this->getUploadPath(),
						'use_upload_name' => true,
						'use_upload_extension' => true,
						'overwrite' => true,
					),
					),
				),
				'validators' => array(
					/*
					array('name' => 'Zend\Validator\File\IsImage'),
					*/
				),
				
				)));

				$inputFilter->add($factory->createInput(array(
				'name' => 'price',
				'required' => true,
				'validators' => array(
					array('name' => 'Digits'),
				),
			)));

			$this->inputFilter = $inputFilter;
		}
		return $this->inputFilter;
    }
    
    public function getArrayCopy()
    {
		return get_object_vars($this);
    }
	
	/**
	 *
	 * @var string
	 */
	protected $uploadPath;
	
	public function setUploadPath($uploadPath)
	{
		$this->uploadPath = $uploadPath;
	}
	
	public function getUploadPath()
	{
		return $this->uploadPath;
	}
}